*WHAT'S ACROSS THE BORDER?
*JHA, NEUMARK, AND RODRIGUEZ-LOPEZ

include directories.do


set level 90, perm

***********************************************
*Regressions for period 1990-upto
local upto 2016

*Leads-lags
local leads_lags "lmw_f2 lmw_f1 lmw_l0 lmw_l1 lmw_l2 lmw_l3 lmw_l4"
local ll 7
***********************************************

putexcel set "`outfigdir'`s'figure4_tabA4.xlsx", replace

foreach d in "emp" {

use "`datadir'`s'cbp_county_state_20ind_sample.dta", clear

*Remove DC (starts in 2004)
drop if state==11

*Keep restaurant industry only
keep if wagerank90==1


* Generating variables 

gen mw=max_mw
gen lmw=log(mw)

tsset cty_fips year


* Leads and lags

forval j = 1(1)3 {
 
	gen lmw_l`j' = S.L`j'.lmw
      if `j' < 4 {
         gen lmw_f`j' = S.F`j'.lmw
       }
} 


gen lmw_l4 = L4.lmw
gen lmw_l0 = S.lmw


*Generate totempm, which removes emp of the industry of interest
foreach b in "emp" {
gen tot`b'm=tot`b'-`b'
}

foreach b in "emp" "wageap" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
gen ltot`b'm=log(tot`b'm)
}

gen lworkagepop=log(workagepop)

**Generate YEAR DUMMIES 
qui tab year, gen(yeard)

 *Dummies region-year
qui tab region, gen(regiond)
gen regionyear=region*10000+year
qui tab regionyear, gen(regionyeard)

*State dummies
qui tab state, gen(stated)
gen trend=year-1989
forvalues z = 1/48 {
gen statedtrend`z'=stated`z'*trend
}

**************generating conditions and controls*************
*local window1  year==1990 | year==1991 | year==1992 | year==1993 | year==1994 | year==1995 | year==1996 | year==1997 | year==1998  | year==1999 | year==2000 | year==2001 | year==2002 | year==2003 | year==2004 | year==2005 | year==2006 | year==2007 | year==2008 | year==2009 | year==2010 | year==2011 | year==2012 | year==2013 | year==2014 | year==2015 | year==2016
local window1 "year<=`upto'"

* Specify clustering
local options "vce(cluster state)"
	
* Specify outcome/regressor/instrument
local xvar "lmw"

* Specify controls
local year_controls yeard*
local regionyear_controls regionyeard*
local statetrend_controls statedtrend*


/* Regression specs */

local yvar "l`d'"
local czone_controls "ltot`d'm lworkagepop"


eststo m1_`u': reghdfe `yvar' `leads_lags' `czone_controls'  if  (`window1') , `options' absorb(cty_fips year)

mat B = e(b)
mat V = e(V)
mat all=r(table)
forval j = 1/`ll' {
	mat est1_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}

}

***DLR PAIR REGRESSIONS



foreach u in "county" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

if "`u'" == "county" {
merge m:1 countypair_id county using "`countydir'`s'countypairtypes.dta"
keep if type0==1
drop type*
drop _merge
}

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'


***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************

egen idvar=group(`u'pair state)

gen lmw=log(mw)

tsset idvar year

* Leads and lags

forval j = 1(1)3 {
 
	gen lmw_l`j' = S.L`j'.lmw
      if `j' < 4 {
         gen lmw_f`j' = S.F`j'.lmw
       }
} 


gen lmw_l4 = L4.lmw
gen lmw_l0 = S.lmw

*Generate totestm and totempm, which remove the est or emp of the industry of interest
gen totestm=totest-est
gen totempm=totemp-emp

foreach b in "emp" "wageap" "wageqp" "est" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
}

gen ltotwageapm=log(totwageapm)
gen ltotwageqpm=log(totwageqpm)
gen ltotestm=log(totestm)
gen ltotempm=log(totempm)
gen ltotpop=log(totpop)
gen lworkagepop=log(workagepop)
gen trend=year-1989

********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local popvar lworkagepop
local totempvar ltotempm
local totwagevar ltotwageapm
local wagevar lwageap
local empvar lemp

**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************


sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')

*USE DATA UP TO 2006
drop if `period'>`upto'

*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications

foreach h in "emp" {

local controls "`tot`h'var' `popvar'"


eststo m2_`h': reghdfe ``h'var' `leads_lags' `controls', `options'
mat B = e(b)
mat V = e(V)
mat all=r(table)
forval j = 1/`ll' {
	mat est2_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}

eststo m3_`h': reghdfe ``h'var' `leads_lags' `controls' if pairtimes==2,  `options'
mat B = e(b)
mat V = e(V)
mat all=r(table)
forval j = 1/`ll' {
	mat est3_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}

}


}

matrix fullmat1 = [est1_emp_1, est2_emp_1, est3_emp_1 ] 

forval j = 2/`ll' {

 matrix fullmat1 = [fullmat1\  [est1_emp_`j', est2_emp_`j', est3_emp_`j' ]]
}

matrix list fullmat1, format(%9.7f)




************************* COMMUTING ZONE SAMPLES ************************

foreach d in "emp" {


* Load data at the czone-state-industry level
use "`datadir'`s'cbp_czone_state_20ind_sample.dta", clear

*Remove DC (starts in 2004)
drop if czone==11304 & state==11

*Keep restaurant industry
keep if wagerank90==1

egen czonestate=group(czone state)


* Generating variables 

gen mw=max_mw
gen lmw=log(mw)

tsset czonestate year

* Leads and lags

forval j = 1(1)3 {
 
	gen lmw_l`j' = S.L`j'.lmw
      if `j' < 4 {
         gen lmw_f`j' = S.F`j'.lmw
       }
} 


gen lmw_l4 = L4.lmw
gen lmw_l0 = S.lmw


*Generate totestm and totempm, which remove the est or emp of the industry of interest
gen totestm=totest-est
gen totempm=totemp-emp

foreach b in "emp" "wageap" "wageqp" "est" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
}

gen ltotwageapm=log(totwageapm)
gen ltotwageqpm=log(totwageqpm)
gen ltotestm=log(totestm)
gen ltotempm=log(totempm)
gen ltotpop=log(totpop)
gen lworkagepop=log(workagepop)

**Generate YEAR DUMMIES 
qui tab year, gen(yeard)

 *Dummies region-year
qui tab region, gen(regiond)
gen regionyear=region*10000+year
qui tab regionyear, gen(regionyeard)

*State dummies
qui tab state, gen(stated)
gen trend=year-1989
forvalues z = 1/48 {
gen statedtrend`z'=stated`z'*trend
}

**************generating conditions and controls*************
*local window1  year==1990 | year==1991 | year==1992 | year==1993 | year==1994 | year==1995 | year==1996 | year==1997 | year==1998  | year==1999 | year==2000 | year==2001 | year==2002 | year==2003 | year==2004 | year==2005 | year==2006 | year==2007 | year==2008 | year==2009 | year==2010 | year==2011 | year==2012 | year==2013 | year==2014 | year==2015 | year==2016
local window1 "year<=`upto'"

* Specify clustering
local options "vce(cluster state)"
	
* Specify outcome/regressor/instrument
local xvar "lmw"

* Specify controls
local year_controls yeard*
local regionyear_controls regionyeard*
local statetrend_controls statedtrend*

/* Regression specs */

local yvar "l`d'"
local czone_controls "ltot`d'm lworkagepop"


eststo n1_`u': reghdfe `yvar' `leads_lags' `czone_controls'  if  (`window1') , `options' absorb(czonestate year)

mat B = e(b)
mat V = e(V)
mat all=r(table)
forval j = 1/`ll' {
	mat est1_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}

}

***DLR PAIR REGRESSIONS



foreach u in "czonestate" {
* Load stacked pair data at the county or czone-state level
use "`datadir'`s'cbp_stacked_`u'pair_sample.dta", clear

************************************************
*******COUNTY-PAIRS OR MULTI-STATA CZONE PAIRS
************************************************

*** PAIR ID VARIABLE (EITHER CZONESTATEPAIR OR COUNTYPAIR)
local pair `u'pair


*Entity variable (EITHER CZONESTATE OR COUNTY)
local geo `u'



***********************************************
*GENERATE VARIABLES (SAME FOR COUNTY OR CZONE)
***********************************************

egen idvar=group(`u'pair state)

gen lmw=log(mw)

tsset idvar year

* Leads and lags

forval j = 1(1)3 {
 
	gen lmw_l`j' = S.L`j'.lmw
      if `j' < 4 {
         gen lmw_f`j' = S.F`j'.lmw
       }
} 


gen lmw_l4 = L4.lmw
gen lmw_l0 = S.lmw

*Generate totestm and totempm, which remove the est or emp of the industry of interest
gen totestm=totest-est
gen totempm=totemp-emp

foreach b in "emp" "wageap" "wageqp" "est" {

gen l`b'=log(`b')
gen ltot`b'=log(tot`b')
}

gen ltotwageapm=log(totwageapm)
gen ltotwageqpm=log(totwageqpm)
gen ltotestm=log(totestm)
gen ltotempm=log(totempm)
gen ltotpop=log(totpop)
gen lworkagepop=log(workagepop)
gen trend=year-1989


********************************************
**** DEFINE LOCAL VARIABLES
********************************************

local period year

*Dependent variable and regressors (SAME FOR COUNTY OR CZONE)

local popvar lworkagepop
local totempvar ltotempm
local totwagevar ltotwageapm
local wagevar lwageap
local empvar lemp

**************************************************
****STACKED REGRESSIONS WITH REGHDFE
**************************************************


sort  `pair' `period'
 
gen state_a = statea
gen state_b = stateb

gen st_min = min(state_a, state_b)
gen st_max = max(state_a, state_b)
egen bordersegment = group(st_min st_max)
egen pairperiod=group(`pair' `period')

*USE DATA UP TO 2006
drop if `period'>`upto'

*Specify clustering
local options "vce(cluster state bordersegment) absorb(`u' pairperiod)"

*Specifications

foreach h in "emp" {

local controls "`tot`h'var' `popvar'"

eststo n2_`h': reghdfe ``h'var' `leads_lags' `controls', `options'
mat B = e(b)
mat V = e(V)
mat all=r(table)
forval j = 1/`ll' {
	mat est2_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}

eststo n3_`h': reghdfe ``h'var' `leads_lags' `controls' if pairtimes==2,  `options'
mat B = e(b)
mat V = e(V)
mat all=r(table)
forval j = 1/`ll' {
	mat est3_emp_`j' = [B[1,`j'], all[5,`j'],all[6,`j']]
}

}



}

matrix fullmat2 = [est1_emp_1, est2_emp_1, est3_emp_1 ] 

forval j = 2/`ll' {

 matrix fullmat2 = [fullmat2\  [est1_emp_`j', est2_emp_`j', est3_emp_`j' ]]
}

matrix list fullmat2, format(%9.7f)



/* Print results */

tempfile specs

#delimit ;
esttab m* n* using "`specs'.csv",
	nodepvars
	collabels(, none)
	mlabels(, none)
	b(%9.3f) se(%9.3f)
	star(* .10 ** .05 *** .01)
	title("Long-term MW responses")
	order(`leads_lags' `controls' )
	nonotes
	label
	stats(N, labels("N") fmt(0))			
	replace;
#delimit cr

* Switch the output to Excel format
insheet using "`specs'.csv", comma clear
export excel "`outdir'`s'TablesJNR.xlsx", sheetreplace sheet(TableA4)

putexcel A1 = matrix(fullmat1)
putexcel J1 = matrix(fullmat2)

clear all
